<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\UserModel;
use think\Validate;

class User extends Base
{
    protected $model;

    public function __construct(UserModel $userModel)
    {
        parent::__construct();
        $this->model = $userModel;
    }

    /**
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }

    /**
     * @return \think\response\View
     */
    public function userAdd()
    {
        return view();
    }

    /**
     * 修改页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userEdit()
    {
        $params = $this->request->param();
        if(!empty($params['id'])){
            $info = $this->model->where('id',$params['id'])->find();
            $this->assign('info',$info);
        }
        return view();
    }

    /**
     * 列表接口
     */
    public function getUserInfo()
    {
        $param = $this->request->param();

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $arr = ['like'=>'user.name'];
        $where = $this->select_param($arr,$param);
        $list = $this->model->getUserListPage($page,$pageSize,$where,false);
        $count = $this->model->getUserListPage(0,0,$where,true);
        Helper::returnJson(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }


    /**
     * 添加 修改
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveUserInfo()
    {
        $param = $this->request->param();
        $rule = [
            'name|姓名'=>'require',
            'sex|性别'=>'require',
            'age|年龄'=>'require',
            'phone|手机号'=>'require|/^[1][3,4,5,6,,7,8,9][0-9]{9}$/',
            'project_id|所属项目'=>'require|number',
            'interview_company|面试公司'=>'require',
            'secret_number|密号'=>'require',
            'id_number|身份证号'=>'require|length:18',
            'is_social|是否交社保'=>'require',
            'social_base|社保基数'=>'require',
            'social_area|社保地区'=>'require',
            'is_stay|是否住宿'=>'require',
            'is_contract|是否签合同'=>'require',
            'admin_id|所属人'=>'require',
            'depart_id|入职部门'=>'require',
            'is_contract|招聘方式'=>'require',
            'status|状态'=>'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            Helper::returnJson(['code'=>500,'msg'=>$validate->getError()]);
        }
        $data = [
            'name'=> $param['name'],
            'sex'=> $param['sex'],
            'age'=> $param['age'],
            'phone'=> $param['phone'],
            'project_id'=> $param['project_id'],
            'interview_company'=> $param['interview_company'],
            'secret_number'=> $param['secret_number'],
            'id_number'=> $param['id_number'],
            'is_social'=> $param['is_social'],
            'social_base'=> $param['social_base'],
            'social_area'=> $param['social_area'],
            'is_stay'=> $param['is_stay'],
            'is_contract'=> $param['is_contract'],
            'admin_id'=> $param['admin_id'],
            'depart_id'=> $param['depart_id'],
            'status'=> $param['status'],
        ];

        if(!empty($param['id'])){
            $data['update_ts'] = afterTime();
            $res = $this->model->where('id',$param['id'])->update($data);
        }else{
            $data['created_ts'] = afterTime();
            $res = $this->model->insert($data);
        }
        if($res!==false){
            Helper::returnJson(['code'=>200,'msg'=>'更新成功','data'=>$res]);
        }else{
            Helper::returnJson(['code'=>500,'msg'=>'error','data'=>$res]);
        }
    }

    /**
     * 删除用户
     */
    public function delUser()
    {
        $param = $this->request->param();
        $info = $this->model->where('id',$param['id'])->update(['deleted'=>time()]);
        if($info){
            Helper::returnJson(['code'=>200,'msg'=>'删除成功']);
        }else{
            Helper::returnJson(['code'=>500,'msg'=>'删除失败']);
        }

    }

}